import AcroMLEngine from '../acroML.Engine.mjs';
import acroMLCulture from '../acroML.Culture.mjs';
import {DxMenu} from 'devextreme-vue/menu';

let AcroMLVueSelector={
  components:{DxMenu},
  data(){
    let self=this;
    let LCIDs=[];
    let isFoundEnglish=false;
    for(let LCID in AcroMLEngine.instance.dd.LCIDList){
      if (LCID==1033) isFoundEnglish=true;
      LCIDs.push(LCID);
    }
    if (!isFoundEnglish){
      LCIDs.unshift(1033);
    }
    // let langs=LCIDs.map(function(LCID){
    //   let lang=acroMLCulture.findCultureByLCID(LCID);
    //   return lang;
    // });
    let langText=AcroMLEngine.instance.LCID;
    let lang=acroMLCulture.findCultureByLCID(AcroMLEngine.instance.LCID);
    if (lang) langText=this.getLangText(lang);
    let langsMenu=[{id:'LanguageRoot',name:t('Language')+': '+langText,items:[]}];
    langsMenu[0].items=LCIDs.map(function(LCID){
      let lang=acroMLCulture.findCultureByLCID(LCID);
      return {id:lang.LCID,name:self.getLangText(lang),type:'lang'}
    });
    return {
      langsMenu,
      AcroMLEngine
    }
  },
  methods:{
    onLangItemClick(e){
      if (e.itemData.type=='lang'){
        let LCID=e.itemData.id;
        AcroMLEngine.instance.LCID=LCID;
        AcroMLEngine.onSwitchLanguage();
        let langText=AcroMLEngine.instance.LCID;
        let lang=acroMLCulture.findCultureByLCID(AcroMLEngine.instance.LCID);
        if (lang) langText=this.getLangText(lang);
        this.langsMenu[0].name=langText;
      }
    },
    getLangText(lang){
      return lang.LCID+','+lang.LanguageName_Native+','+lang.RegionName_Native;
    }
  },
  template:`
    <div>
      <DxMenu
        :data-source="langsMenu"
        :show-first-submenu-mode="{
          name: 'onHover',
          delay: { show: 0, hide: 0 },
        }"
        :hide-submenu-on-mouse-leave="true"
        orientation="horizontal"
        display-expr="name"
        @item-click="onLangItemClick"
      />
    </div>
  `
}
export default AcroMLVueSelector;
let VueSelector=AcroMLVueSelector;
export {VueSelector};